package org.kl.bf.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MoneyUtil {

	private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
	private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
	private static final double MAX_VALUE = 9999999999999.99D;

	// 方法二
	private static String[] CH = { "", "", "拾", "佰", "仟" };
	private static String[] CHS_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	private static String[] CHS = { "万", "亿", "兆" };
	private static DecimalFormat df = new DecimalFormat("#########################0.0#");

	public static String getChineseNumber(double i) {
		return DIGIT.charAt((int) i) + "";
	}

	public static String change(double v) {
		if (v < 0 || v > MAX_VALUE) {
			return "参数非法!";
		}
		long l = Math.round(v * 100);
		if (l == 0) {
			return "零元整";
		}
		String strValue = l + "";
		// i用来控制数
		int i = 0;
		// j用来控制单位
		int j = UNIT.length() - strValue.length();
		String rs = "";
		boolean isZero = false;
		for (; i < strValue.length(); i++, j++) {
			char ch = strValue.charAt(i);
			if (ch == '0') {
				isZero = true;
				if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
					rs = rs + UNIT.charAt(j);
					isZero = false;
				}
			} else {
				if (isZero) {
					rs = rs + "零";
					isZero = false;
				}
				rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
			}
		}
		if (!rs.endsWith("分")) {
			rs = rs + "整";
		}
		rs = rs.replaceAll("亿万", "亿");
		return rs;
	}

	// 方法二
	public static String transFormMoney(double money) {
		return transFormMoney(df.format(money));
	}

	public static String transFormMoney(float money) {
		return transFormMoney(df.format(money));
	}

	public static String transFormMoney(String money) {
		String result = "";

		try {
			BigDecimal big = new BigDecimal(money);
			String[] t = null;
			try {
				t = big.toString().replace(".", ";").split(";");
			} catch (Exception e) {
				// 金额如果没有小数位时,也要加上小数位
				t = (big.toString() + ".0").replace(".", ";").split(";");
			}

			String[] intString = splitMoney(t[0]);
			String tmp_down = t[1];

			for (int i = 0; i < intString.length; i++) {
				result = result + count(intString[i]);

				if (i == intString.length - 2 || i == intString.length - 3)
					continue;

				if (i != intString.length - 1) {
					result = result + CHS[intString.length - 2 - i];
				}
			}

			if (Integer.parseInt(tmp_down) == 0) {
				result = result + (intString[0].equals("0") ? "零元" : "元整");
			} else {
				result = result + (intString[0].equals("0") ? "" : tmp_down.startsWith("0") ? "元零" : "元") + getFloat(tmp_down);
			}

		} catch (Exception e) {
			return "你輸入的不是數字符串";
		}

		return result;
	}

	public static String[] splitMoney(String money) {
		StringBuffer tmp_int = new StringBuffer();
		tmp_int.append(money);

		int i = tmp_int.length();

		do {
			try {
				i = i - 4;

				if (i == 0)
					break;

				tmp_int.insert(i, ';');

			} catch (Exception e) {
				break;
			}
		} while (true);

		return tmp_int.toString().split(";");
	}

	public static String count(String money) {
		String tmp = "";
		Integer money_int = 0;
		char[] money_char;

		if (money.startsWith("0")) {
			money_int = Integer.parseInt(money);

			if (money_int == 0)
				return tmp;
			else
				tmp = "零";

			money_char = money_int.toString().toCharArray();
		} else {
			money_char = money.toCharArray();
		}

		for (int i = 0; i < money_char.length; i++) {
			if (money_char[i] != '0') {
				tmp = tmp + CHS_NUMBER[Integer.parseInt(money_char[i] + "")] + CH[money_char.length - i];
			} else {
				if (!tmp.endsWith("零") && i != money_char.length - 1 && money_char[i + 1] != '0') {
					tmp = tmp + CHS_NUMBER[Integer.parseInt(money_char[i] + "")];
				}
			}
		}

		return tmp;
	}

	private static String getFloat(String fl) {
		String f = "";
		char[] ch = fl.toCharArray();

		switch (ch.length) {
			case 1: {
				f = f + CHS_NUMBER[Integer.parseInt(ch[0] + "")] + "角整";
				break;
			}

			case 2: {
				if (ch[0] != '0')
					f = f + CHS_NUMBER[Integer.parseInt(ch[0] + "")] + "角" + CHS_NUMBER[Integer.parseInt(ch[1] + "")] + "分";
				else
					f = f + CHS_NUMBER[Integer.parseInt(ch[1] + "")] + "分";

				break;
			}
		}
		return f;
	}
}